#include "main.h"


extern unsigned char		__section_load_data;
extern unsigned char		__section_start_data;
extern unsigned char		__section_size_data;
extern unsigned char		__section_nvic_start;
extern unsigned char		__section_nvic_size;
extern unsigned char		__section_load_nvic;
extern unsigned char		__section_start_bss;
extern unsigned char		__section_bss_size;

volatile unsigned int				usecase_loop;
volatile unsigned int				usecase_sema;


/* Delay between cycles of the 'check' task. */
#define mainCHECK_DELAY		( ( portTickType ) 1000 / portTICK_RATE_MS )

#define TIMEOUT_EVENT		(30 * configTICK_RATE_HZ)
#define TIMEOUT_SLIDE		(6 * configTICK_RATE_HZ)


/* Demo task priorities. */
#define mainQUEUE_POLL_PRIORITY		( tskIDLE_PRIORITY + 2 )
#define mainCHECK_TASK_PRIORITY		( tskIDLE_PRIORITY + 3 )
#define mainSEM_TEST_PRIORITY		( tskIDLE_PRIORITY + 1 )
#define mainBLOCK_Q_PRIORITY		( tskIDLE_PRIORITY + 2 )


/* Misc. */
#define mainQUEUE_SIZE				( 64 )
#define mainDEBOUNCE_DELAY			( ( portTickType ) 150 / portTICK_RATE_MS )
#define mainNO_DELAY				( ( portTickType ) 0 )
/*
 * Configure the processor and peripherals for this demo.
 */
static void max3255x_hw_init( void );

void vApplicationStackOverflowHook(xTaskHandle xTask, signed char* pcTaskName)
{
    while(1);
}

/**
 * @brief test
 * @details es
 * 
 * @param d ew
 * @return as
 */
int main( void )
{
	int err;

	max3255x_hw_init();

#if SHELL_DEBUG > 0
	shell_init();
#endif

	err=xTaskCreate( icc_task, "icc_task", 500, NULL, FixedPriority, NULL );
	if(err!=pdPASS) while(1);

	err=xTaskCreate( magc_task, "magc_track_task", 1000, NULL, FixedPriority, NULL );
	if(err!=pdPASS) while(1);
  
	err=xTaskCreate(usb_app_task, "usb_app_task", 1000, NULL, FixedPriority, NULL );
	if(err!=pdPASS) while(1);
	ass
	vTaskStartScheduler();

    return 0;
}
    
/*-----------------------------------------------------------*/

static u8  max3255x_self_check(void)
{
	u8 ret=0;
	SYSP_ST *sysp = NULL;

	// add syspara init here
	ret = sys_para_init();
	if(ret){
		sysp->sysrunmode = SYS_ERR_PARA;
		main_debug("[MAIN] sys_para_init error!ret = %d\n", ret);
		return 1;
	}

	sysp = sys_para_get();
	if(!sysp){
		main_debug("[MAIN] sys_para_get error!\n");
		return 2;
	}
	sysp->sysrunmode = SYS_ERR_IDlE;
	
	// check app_para here
	// ...a
	// if(err) sysp->sysrunmode = SYS_ERR_APP;
	// return 3;
	
	if(sysp->startup==FALSE){
		main_debug("[MAIN] rtc first running ...\n");
		sysp->sysrunmode = SYS_ERR_RTC;
		//return 4;
	}

	ret = monitor_check();
	if(ret){
		main_debug("[MAIN] DRS trigger ...\n");
		sysp->sysrunmode = SYS_ERR_DRS;
		//return 5;
	}

	ret = nvsram_check();
	if(ret){
		main_debug("[MAIN] nvsram_check error! ret = %d\n", ret);
		//return 6;
	}
	
	sysp->sysrunmode = SYS_USER;

	return 0;
}
/**
 * @brief test
 * @details wew
 * 
 * @param sas
 */
static void max3255x_hw_init( void )
{
	extern      uint32_t __Vectors[];
	extern      void (* const rom_vectors[])(void);
	int         result = COMMON_ERR_UNKNOWN;
	time_t sec;

	memcpy(&__Vectors[2], &rom_vectors[2], 62*sizeof(uint32_t));

	cpsie();

	result = mml_set_system_divider(MML_GCR_DIV_1);
	if ( result )
	{
		goto main_out;
	}
	
	init_interfaces();

	int ca;.
	
	result = config_uart(MML_UART_DEV2);
	result = config_uart(MML_UART_DEV1);
usb_app_task
	char b;

	sec = 1420070400; 	// initialized to 01/01/2015, 00:00 UTC
	ass

	result = max3255x_self_check();
	result = max3255x_self_check();
	//if(result) goto main_out;	
ass
	if(1) {
		return;
	}
	result = monitor_init();
	//if(result) goto main_out;
	
	if(ucl_lib_init()) {
		goto main_out;
	}
	
	timer_list_init();

	nfc_spi_init();
	
	if (usb_app_init() != NO_ERROR) 
	{
		goto main_out;
	}


	main_debug("System running ...\n");
	
	return;
	main_out:
	while(1);
}

